package com.example.mybatisplus.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplus.stock.dto.InStockDto;
import com.example.mybatisplus.stock.entity.Stock;
import com.example.mybatisplus.stock.entity.StockLog;
import com.example.mybatisplus.stock.mapper.StockLogMapper;
import com.example.mybatisplus.stock.mapper.StockMapper;
import com.example.mybatisplus.stock.service.StockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;


import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwzheng
 * @since 2021-05-01
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {

    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private StockLogMapper stockLogMapper;

    //入库
    @Override
    public void instock(InStockDto stockDto) {

        Integer buyQuantity = stockDto.getBuyQuantity();
        Integer inStockQuantity = stockDto.getInStockQuantity();
        StockLog stockLog = StockLog.builder().productId(stockDto.getProductId())
                .productName(stockDto.getProductName())
                .productSize(stockDto.getProductSize())
                .buyId(stockDto.getBuyId())
                .fluDirection("1")
                .build();
        QueryWrapper<StockLog> queryWrapper = new QueryWrapper<>(stockLog);
        List<StockLog> stockLogs = stockLogMapper.selectList(queryWrapper);
        //現有入庫的总数量
        int currentQuantity = stockLogs.stream().mapToInt(StockLog::getQuantity).sum();
        Assert.isTrue(currentQuantity >= buyQuantity,"改产品已经全部入库，数量已满");
        Assert.isTrue(currentQuantity + inStockQuantity > buyQuantity,
                "现存库存和此次入库数量之和大于最大采购量了");
        //入库，查看库存情况
        Stock stock = Stock.builder().productId(stockDto.getProductId())
                .productName(stockDto.getProductName())
                .productSize(stockDto.getProductSize())
                .build();
        QueryWrapper<Stock> stockQueryWrapper = new QueryWrapper<>(stock);
        Stock currentSock = stockMapper.selectOne(stockQueryWrapper);

        //第一次入库新增
        if(currentSock == null || currentSock.getStock() == 0){
            stock.setStock(inStockQuantity);
            stock.setCreateUser(stockDto.getCreateUser());
            stock.setCreateTime(LocalDateTime.now());
            stock.setUpdateUser(stockDto.getCreateUser());
            stock.setUpdateTime(LocalDateTime.now());
            stockMapper.insert(stock);
         }else{
         //第二次入库更新
         //新增入库后总数量
            int updatedStock = currentSock.getStock() + inStockQuantity;
            currentSock.setStock(updatedStock);
            stockMapper.updateById(currentSock);
        }

        //保存到log
        StockLog insertStockLog = StockLog.builder().seqId(UUID.randomUUID().toString())
                .productId(stockDto.getProductId())
                .productName(stockDto.getProductName())
                .productSize(stockDto.getProductSize())
                .buyId(stockDto.getBuyId())
                .fluDirection("1")
                .quantity(inStockQuantity)
                .createUser(stockDto.getCreateUser())
                .createTime(LocalDateTime.now())
                .updateUser(stockDto.getCreateUser())
                .updateTime(LocalDateTime.now())
                .build();

           stockLogMapper.insert(insertStockLog);


    }
}
